package cn.zhibang.sannuo.pharmacy.api;

import cn.zhibang.sannuo.account.service.AAccountnumderService;
import cn.zhibang.sannuo.commons.model.Accountnumber;
import cn.zhibang.sannuo.commons.model.BranchOffice;
import cn.zhibang.sannuo.commons.model.CommonResult;
import cn.zhibang.sannuo.commons.model.HeadOffice;
import cn.zhibang.sannuo.pharmacy.service.HeadOfficeService;
import cn.zhibang.sannuo.pharmacy.service.PBranchOfficeService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName PharmacyApi.java
 * @Description TODO
 * @createTime 2021年04月06日 14:04:00
 */
@RestController
public class PharmacyApi {

    @Autowired private HeadOfficeService headOfficeService;
    @Autowired private CommonResult commonResult;
    @Autowired private AAccountnumderService aAccountnumderService;
    @Autowired private PBranchOfficeService branchOfficeService;

    @PostMapping("/getHeadOffice")
    @ApiOperation("获得总店信息")
    public CommonResult getHeadOffice(String hname, String hnumder){
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        String rnumber = aAccountnumderService.selAccountnumberRnumber(accountnumber.getAccount());
        if ("CJ".equals(rnumber)||"PT".equals(rnumber)){  //超级管理员
            if (hname == "")hname=null;
            if (hnumder == "")hnumder=null;
            List<HeadOffice> headOffices = headOfficeService.selAllHeadOffice(hname, hnumder);
            commonResult.setData(headOffices);
        }
        if ("ZD".equals(rnumber)){  //总店管理员
            List<HeadOffice> headOffices = headOfficeService.selAllHeadOffice(accountnumber.getBranchOffice().getBname(), null);
            commonResult.setData(headOffices);
        }
        return commonResult;
    }

    @PostMapping("/upHeadOffice")
    @ApiOperation("修改总店信息")
    public void upHeadOffice(HeadOffice headOffice){
        if (headOffice.getHname()=="")headOffice.setHname(null);
        if (headOffice.getHcontacts()=="")headOffice.setHcontacts(null);
        if (headOffice.getHphone()=="")headOffice.setHphone(null);
        if (headOffice.getHtelephone()=="")headOffice.setHtelephone(null);
        headOfficeService.upHeadOffice(headOffice);
    }

    @PostMapping("/addHeadOffice")
    @ApiOperation("添加总店")
    public CommonResult addHeadOfficeinfo(HeadOffice headOffice,String lianXiphone ,String phone){
        if ("hphone".equals(lianXiphone))headOffice.setHphone(phone);
        if ("htelephone".equals(lianXiphone))headOffice.setHtelephone(phone);
        Accountnumber accountnumber = headOfficeService.addHeadOffice(headOffice);
        commonResult.setCode(200);
        commonResult.setMaggers("总店的账号信息为：");
        commonResult.setData(accountnumber);
        return commonResult;
    }

    @PostMapping("/getBranchOffice")
    @ApiOperation("获得分店店信息")
    public CommonResult getBranchOffice(String bname, String bnumber, String baddress){
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        String rnumber = aAccountnumderService.selAccountnumberRnumber(accountnumber.getAccount());

        Integer hid = accountnumber.getBranchOffice()==null?null:accountnumber.getBranchOffice().getHeadOffice().getHid();
        if (bname == "")bname =null;
        if (bnumber == "")bnumber =null;
        if ("全部全部全部".equals(baddress))baddress = null;
        Integer bleng = baddress==null?null:baddress.length();
        List<BranchOffice> branchOffices;
        if ("FD".equals(rnumber)){  //分店管理员
            branchOffices = branchOfficeService.selAllBranchOffice(hid, accountnumber.getBranchOffice().getBname(), bnumber, baddress, bleng);
        }else{
            branchOffices = branchOfficeService.selAllBranchOffice(hid, bname, bnumber, baddress, bleng);
        }
        commonResult.setData(branchOffices);
        return commonResult;
    }

    @PostMapping("/addBranchOffice")
    @ApiOperation("添加分店")
    public void addBranchOffice(BranchOffice branchOffice,String lianXiphone ,String phone){
        if ("hphone".equals(lianXiphone))branchOffice.setBphone(phone);
        if ("htelephone".equals(lianXiphone))branchOffice.setBtelephone(phone);
        Subject subject = SecurityUtils.getSubject();
        Accountnumber accountnumber = (Accountnumber)subject.getPrincipal();
        BranchOffice showbranchOffice = accountnumber.getBranchOffice();
        HeadOffice headOffice = showbranchOffice.getHeadOffice();
        branchOffice.setHeadOffice(headOffice);
        branchOfficeService.addBranchOffice(branchOffice);
    }

    @PostMapping("/upBranchOffice")
    @ApiOperation("修改分店信息")
    public void upBranchOffice(BranchOffice branchOffice){
        System.out.println(branchOffice);

    }

}
